<template>
  <div class="web-container">
    <div class="controls">
      <input 
        v-model="url" 
        type="text" 
        placeholder="输入网页地址 (如: https://example.com)"
        @keyup.enter="loadWebpage"
      />
      <button @click="loadWebpage">加载</button>
      <button @click="reload" :disabled="!url">刷新</button>
    </div>
    
    <div class="iframe-wrapper">
      <iframe 
        v-if="currentUrl" 
        :src="currentUrl" 
        frameborder="0"
        allowfullscreen
        ref="iframe"
      ></iframe>
      <div v-else class="placeholder">
        <p>请输入网址并点击"加载"按钮</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'WebContainer',
  data() {
    return {
      url: 'http://10.7.4.212/token/WVhSdGNuQjAjUkhVeE1qTTBOVFlx/L3ByZXZpZXdOZXcvYmI3ZTA4NGEtOTE3Ny00ZDhjLThlNGItOGMyZmE2Y2U1Y2U3LzMwNzdiYjE5LTAxMTctNDY2ZS05Y2I4LWE5ZTlhYzRjN2I5NQ==',
      currentUrl: 'http://10.7.4.212/token/WVhSdGNuQjAjUkhVeE1qTTBOVFlx/L3ByZXZpZXdOZXcvYmI3ZTA4NGEtOTE3Ny00ZDhjLThlNGItOGMyZmE2Y2U1Y2U3LzMwNzdiYjE5LTAxMTctNDY2ZS05Y2I4LWE5ZTlhYzRjN2I5NQ=='
    }
  },
  methods: {
    loadWebpage() {
      if (!this.url) return;
      
      // 添加协议前缀如果不存在
      if (!this.url.startsWith('http://') && !this.url.startsWith('https://')) {
        this.url = 'https://' + this.url;
      }
      
      this.currentUrl = this.url;
    },
    reload() {
      if (this.currentUrl) {
        // 通过重新赋值触发iframe重新加载
        const temp = this.currentUrl;
        this.currentUrl = '';
        this.$nextTick(() => {
          this.currentUrl = temp;
        });
      }
    }
  }
}
</script>

<style scoped>
.web-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
}

.controls {
  padding: 10px;
  background: #f5f5f5;
  display: flex;
  gap: 10px;
}

.controls input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.controls button {
  padding: 8px 16px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.controls button:disabled {
  background: #cccccc;
  cursor: not-allowed;
}

.iframe-wrapper {
  flex: 1;
  position: relative;
}

iframe {
  width: 100%;
  height: 100%;
}

.placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  color: #666;
}
</style>
